#include "CLoger.h"
#include <string.h>
#include <stdio.h>

#include <windows.h>
#include <Shlwapi.h>

#pragma comment(lib, "Shlwapi.lib")

/********************************************************/

bool	CLoger::Log(eLOGER_TYPE type, const char* ccMsg, ...)
{	
	va_list stdarg;
	va_start(stdarg, ccMsg);
	char execPath[MAX_PATH];
	char c_LogFile[MAX_PATH];

	GetModuleFileName(NULL, execPath, MAX_PATH);
	PathRemoveFileSpec(execPath);
	bool __return = false;
	if(type == ERR) sprintf(c_LogFile, "%s\\error.log",execPath);
	if(type == WAR) sprintf(c_LogFile, "%s\\warn.log",execPath);
	if(type == NOR) sprintf(c_LogFile, "%s\\normal.log",execPath);

	SYSTEMTIME systime;
	GetSystemTime(&systime);
					 
	FILE * fp = fopen(c_LogFile, "a+");
	if(!LOG_PROGRAM_STARTED){
		fprintf(fp, "%i-%i-%i-------------------------------------------------\n",
			systime.wDay,systime.wMonth,systime.wYear);
		LOG_PROGRAM_STARTED = true;
	}
	fprintf(fp, "[ %i:%i:%i ]\t",systime.wHour,systime.wMinute,systime.wSecond);
#if (defined(_CONSOLE) && defined(_DEBUG))
	printf("\nLog data:\n");
	vprintf(ccMsg, stdarg);
	printf("\n");
#endif

#if (!defined(_DEBUG) && defined(_CONSOLE))	
	vprintf(ccMsg, stdarg);	
#endif

	vfprintf(fp, ccMsg,stdarg);
	fputc('\n',fp);
	fclose(fp);	
	
	  /*
#ifdef _DEBUG
	char msg[256];
	vsprintf(msg, ccMsg,stdarg);
	MessageBox(NULL, msg, "Error!", MB_OK | (type==ERR ? MB_ICONSTOP : (type==WAR ? MB_ICONEXCLAMATION: 0 ) ));
#endif
	*/
	va_end(stdarg);
	return __return;
}
